//获取关键字
var keyword = getQueryString('id');

//angular模块开发
var app = angular.module("myApp",["globalApp"]);

app.directive('listScroll',function(){
    return {
        restrict:'E',
        template:   '<div class="swiper-container">' +
			    '<div class="swiper-wrapper">' +
			    '<div class="swiper-slide" ng-repeat=" k in imgArr track by $index" repeat-finish="renderFinish()"><img ng-src="{{ k }}"/></div>' +
			    '</div>' +
			    '<!-- 如果需要分页器 -->' +
			    '<div class="swiper-pagination"></div>' +
			    '</div>' ,
        transclude: true,
        link:function($scope,element,attrs){
            $scope.renderFinish  = function(){
                var mySwiper = new Swiper ('.swiper-container', {
			        pagination: '.swiper-pagination',
			        paginationClickable: true,
			        autoplay: 2000,
			        speed:800,
				    observer:true,//修改swiper自己或子元素时，自动初始化swiper
		            observeParents:true,//修改swiper的父元素时，自动初始化swiper
			        // 如果需要分页器
			        pagination: '.swiper-pagination'
				}) 
        	}
              
    	}
    }
})

//自定义指令,ng-repeat完成后运行
app.directive('repeatFinish',function(){
	return {
        link: function(scope,element,attr){
            if(scope.$last == true){
                scope.$eval( attr.repeatFinish );
            }
        }
    }
})

//设置一个过滤器，将字符串转化为html
app.filter('trust2Html', ['$sce',function($sce) {  
    return function(val) {  
        return $sce.trustAsHtml(val);   
    };  
}])  

//angular的控制台
app.controller("myCtrl",function($scope,$http){
	$scope.reminder = false;  //隐藏提示框
	$scope.close = function(){
		$scope.reminder = false;  //隐藏提示框
	}
	
	var params = {
	    code : keyword
	};
	params = $.param(params);
	$http({
	        method : "post",
	        dataType: "json",
			withCredentials: true,	//跨域名请求。XHR：XMLHttpRequest (XHR) ，基于XML技术的Http请求
		    crossDomain: true,
		    headers:{'Content-Type': 'application/x-www-form-urlencoded'},
		    data:params,
	        async : true,
	        url : host + "/terminal/goods/details"
    }).success(function(response){
	    if(response.code == 2000){
	        $scope.imgArr =  response.data.goods.imageList.split(",");  //轮播图
	        $scope.arr = response.data.goods;  //商品介绍
	        $scope.details = JSON.parse(response.data.goods.specifications);  //转JSON格式
	        $scope.specificationsArr = [];   //用来存储specifications里面的信息
	        for(var i in $scope.details){
	        	$scope.specificationsArr.push(i);
	        }
	    }
	}).error(function(response){
	    console.log(response);
	});
	
	$scope.setCart = function(){
		var code = keyword;
    	var kNum = 1;
    	//对购物车的请求，查看是否存在该商品，并获得num
    	$http({
    		method : "get",
    		withCredentials: true,	//跨域名请求。XHR：XMLHttpRequest (XHR) ，基于XML技术的Http请求
		    crossDomain: true,
		    async : true,
		    contentType: "application/x-www-form-urlencoded",//表单提交
		    url : host + "/terminal/shoppingCart/list"
    	}).success(function(response){
    		if(response.code == 2000){
    			for(var i=0; i<response.data.shoppingCarts.length; i++){
	    			if(code == response.data.shoppingCarts[i].goods.code){
		    			kNum = response.data.shoppingCarts[i].num + 1;
		    		}
	    		}
    		}
    		if(response.code == 4003){
    			$scope.reminder = true;
    			$scope.message = response.message;
    		}
    	})
    	
    	//添加商品
    	$http({
	        method : "post",
	        dataType: "json",
			withCredentials: true,	//跨域名请求。XHR：XMLHttpRequest (XHR) ，基于XML技术的Http请求
		    crossDomain: true,
		    contentType: "application/x-www-form-urlencoded",//表单提交
		    data:{
		    	goodsCode : code,
		    	num : kNum
		    },
	        url : host + "/terminal/shoppingCart/add"
	    }).success(function(response){
	        if(response.code == 2000){
	        	$scope.reminder = true;
    			$scope.message = response.message;
	        }
	    }).error(function(response){
	    	
	    });
	}
	
	//点击立即购买
	$scope.toBuy = function(){
		var code = keyword;
    	var kNum = 1;
    	//对购物车的请求，查看是否存在该商品，并获得num
    	$http({
    		method : "get",
    		withCredentials: true,	//跨域名请求。XHR：XMLHttpRequest (XHR) ，基于XML技术的Http请求
		    crossDomain: true,
		    async : true,
		    contentType: "application/x-www-form-urlencoded",//表单提交
		    url : host + "/terminal/shoppingCart/list"
    	}).success(function(response){
    		if(response.code == 2000){
    			for(var i=0; i<response.data.shoppingCarts.length; i++){
	    			if(code == response.data.shoppingCarts[i].goods.code){
		    			kNum = response.data.shoppingCarts[i].num + 1;
		    		}
	    		}
    		}
    		if(response.code == 4003){
    			$scope.reminder = true;
    			$scope.message = response.message;
    		}
    	})
    	
    	//添加商品
    	var params = {
	        goodsCode : code,
		    num : kNum
		};
		params = $.param(params);
    	$http({
	        method : "post",
	        dataType: "json",
			withCredentials: true,	//跨域名请求。XHR：XMLHttpRequest (XHR) ，基于XML技术的Http请求
		    crossDomain: true,
		    headers:{'Content-Type': 'application/x-www-form-urlencoded'},
		    async : true,
		    data:params,
	        url : host + "/terminal/shoppingCart/add"
	    }).success(function(response){
	        if(response.code == 2000){
	        	
	        }
	    }).error(function(response){
	    	
	    });
	    
	    //重新获取订单的标志
	    $http({
    		method : "get",
    		withCredentials: true,	//跨域名请求。XHR：XMLHttpRequest (XHR) ，基于XML技术的Http请求
		    crossDomain: true,
		    async : true,
		    contentType: "application/x-www-form-urlencoded",//表单提交
		    url : host + "/terminal/shoppingCart/list"
    	}).success(function(response){
    		if(response.code == 2000){
    			for(var i=0; i<response.data.shoppingCarts.length; i++){
	    			if(code == response.data.shoppingCarts[i].goods.code){
		    			window.sessionStorage.goods = response.data.shoppingCarts[i].code;
		    			location.href = "order_confirmation.html";
		    		}
	    		}
    		}
    		if(response.code == 4003){
    			$scope.reminder = true;
    			$scope.message = response.message;
    		}
    	})
	}
})

